﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Web;
using SSOCommon;
using SSOClient.Config;

namespace SSOClient
{
    public partial class SSOClientCore
    {
        public static string GenerateLoginUrl(string redirectUrl)
        {
            Dictionary<string, object> queryArguments = new Dictionary<string, object>();
            queryArguments.Add(ArgumentConstant.SENDER, (int)SenderSource.Client);
            queryArguments.Add(ArgumentConstant.ACTION, (int)SSOAction.Login);
            queryArguments.Add(ArgumentConstant.TOKEN, StateManager.Token);
            string url = String.Format("{0}{1}", redirectUrl, Tools.ConstructQueryString(queryArguments, redirectUrl.IndexOf("?") > -1));
            return url;
        }

        public static string GenerateAuthUrl(string targetUrl)
        {
            Dictionary<string, object> queryArguments = new Dictionary<string, object>();
            queryArguments.Add(ArgumentConstant.SENDER, (int)SenderSource.Client);
            queryArguments.Add(ArgumentConstant.ACTION, (int)SSOAction.Auth);
            queryArguments.Add(ArgumentConstant.TOKEN, StateManager.Token);
            string url = String.Format("{0}{1}", targetUrl, Tools.ConstructQueryString(queryArguments, targetUrl.IndexOf("?") > -1));
            return url;
        }

        public static string GenerateLogoutUrl()
        {
            Dictionary<string, object> queryArguments = new Dictionary<string, object>();
            queryArguments.Add(ArgumentConstant.SENDER, (int)SenderSource.Client);
            queryArguments.Add(ArgumentConstant.ACTION, (int)SSOAction.Logout);
            queryArguments.Add(ArgumentConstant.TOKEN, StateManager.Token);
            string url = String.Format("{0}{1}", SSOConfig.Instance.LogoutUrl, Tools.ConstructQueryString(queryArguments, false));
            return url;
        }

        public static string GetParamValue(string key)
        {
            return String.IsNullOrEmpty(HttpContext.Current.Request[key]) ? String.Empty : HttpContext.Current.Request[key];
        }

        public static string GetToken()
        {
            return StateManager.Token;
        }
    }
}
